package org.ht.database;

import javax.sql.DataSource;

import org.azj.inireader.BasicIniReader;
import org.azj.logHelper.LOG;
import org.azj.logHelper.LOG_COMPONENT;
import org.azj.logHelper.LOG_LEVEL;

import com.ibm.db2.jcc.DB2SimpleDataSource;

public class DataBase {
	static DataSource db;

	public static DataSource getDataBase() {
		if (db == null) {
			DB2SimpleDataSource db2ds = new DB2SimpleDataSource();

			BasicIniReader iniReader = new BasicIniReader();
			try {
				iniReader.read("config\\LH_DB_Connection.ini");
				LOG.WriteLog("成功读取数据库配置文件");
			} catch (Exception e) {
				LOG.WriteLog(LOG_LEVEL.CRITICAL,
						LOG_COMPONENT.LOG_COMP_INI_FILE_LOADER, "读取数据库配置文件失败");
				return null;
			}

			db2ds.setServerName(iniReader.getValueBySectionKey("DB_CONN",
					"SERVER_NAME"));
			db2ds.setPortNumber(Integer.parseInt(iniReader.getValueBySectionKey("DB_CONN",
					"PORT_NUM")));
			db2ds.setDatabaseName(iniReader.getValueBySectionKey("DB_CONN",
					"DB_NAME"));
			db2ds.setUser(iniReader.getValueBySectionKey("DB_CONN", "USER"));
			db2ds.setPassword(iniReader.getValueBySectionKey("DB_CONN",
					"PASSWORD"));
			db2ds.setLoginTimeout(Integer.valueOf(iniReader.getValueBySectionKey("DB_CONN",
					"LOGIN_TIME_OUT")));

			db = db2ds;
		}

		return db;
	}
}
